package me.helllp.dake.common.security;

import org.apache.commons.codec.binary.Base64;

import java.security.SecureRandom;

public class CryptoUtil {
    private static SecureRandom random;

    static {
        random = new SecureRandom();
    }

    /**
     * 产生IV
     *
     * @return iv值
     */
    public static byte[] genIv() {
        return random.generateSeed(16);
    }

    public static byte[] genKey(int keySize) {
        return random.generateSeed(keySize / 8);
    }

    public static String encode(byte[] date) {
        return Base64.encodeBase64String(date);
    }

    public static byte[] decode(String date) {
        return Base64.decodeBase64(date);
    }

    /**
     * 产生盐值，经过base64转换
     *
     * @param sltLength bit位数
     * @return
     */
    public static String genSalt(int sltLength) {
        return encode(genKey(sltLength));
    }
}
